
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.*;

public class InformationPanel extends JPanel {
    
    private MainFrame main;
    private JLabel segCountLabel, intCountLabel;
    private IntersectionList intList;
    private JScrollPane scroll;
    private JScrollBar hscroll, vscroll;
    private JButton launch;
    
    public InformationPanel(MainFrame main){
        super();
        this.main = main;
        this.setLayout(new BorderLayout());
        this.initTopPanel();
        this.initBottomPanel();
    }
    
    private void initTopPanel(){
        JPanel top = new JPanel();
        JLabel title1 = new JLabel("Nombre de segments:");
        JLabel title2 = new JLabel("Nombre d'intersections");
        segCountLabel = new JLabel("0");
        intCountLabel = new JLabel("0");
        top.setLayout(new BoxLayout(top, BoxLayout.Y_AXIS));
        top.setBorder(BorderFactory.createTitledBorder("Informations"));
        top.add(title1);
        top.add(segCountLabel);
        top.add(title2);
        top.add(intCountLabel);
        this.add(top, BorderLayout.NORTH);
    }
    
    private void initBottomPanel(){
        JPanel bottom = new JPanel();
        bottom.setLayout(new BorderLayout());
        bottom.setBorder(BorderFactory.createTitledBorder("Intersections"));
        launch = new JButton("Calculer");
        launch.addActionListener(new LaunchListener());
        intList = new IntersectionList();
        JList list = new JList(intList);
        scroll = new JScrollPane(list);
        vscroll = scroll.getVerticalScrollBar();
        bottom.add(scroll, BorderLayout.CENTER);
        this.add(bottom, BorderLayout.CENTER);
        this.add(launch, BorderLayout.SOUTH);
    }
    
    
    public void setNumberOfSegments(int count){
        segCountLabel.setText("" + count);
    }
    
    public void setNumberOfIntersections(int count){
        intCountLabel.setText("" + count);
    }
    
    public void setList(ArrayList<Point> intersections){
        intList.add(intersections);
    }
    
    public void clear(){
        segCountLabel.setText("0");
        intCountLabel.setText("0");
        intList.removeAll();
    }
    
    private class LaunchListener implements ActionListener{

        public void actionPerformed(ActionEvent ae) {
            try {
                intCountLabel.setText("0");
                intList.removeAll();
                main.compute();
            } catch (Exception ex) {
                JOptionPane.showConfirmDialog(main, "Une erreur s'est produite", "Attention", JOptionPane.ERROR_MESSAGE);
            }
        }
        
    }
    
}
